import React, { PropsWithChildren } from 'react';
import { observer } from 'mobx-react-lite';
import RootStore from 'store/RootStore';
import { FetchMeta } from 'store/RootStore/ApiStore';

const PrivateRoute: React.FC<PropsWithChildren> = ({ children }) => {
    if (RootStore.githubOAuth.userOwnProfile) {
        return <>{children}</>;
    }
    if (!RootStore.githubOAuth.isLogin && RootStore.githubOAuth.accessTokenMeta !== FetchMeta.FETCHING) {
        window.location.assign(
            `https://github.com/login/oauth/authorize?client_id=${process.env.CLIENT_ID as string}&redirect_uri=${
                window.location.href
            }&scope=${['repo', 'user', 'delete_repo'].join(',')}`,
        );
    }
    return null;
};

export default observer(PrivateRoute);
